package com.itheima.reggie.service.impl;

import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.*;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Rodgers
 * @Date 2022/3/18 16:33
 */
@Service
public class OrderServiceImpl {

    @Autowired(required = false)
    private OrderMapper orderMapper;

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private AddressBookMapper addressBookMapper;

    @Autowired(required = false)
    private OrderDetailMapper orderDetailMapper;

    @Autowired(required = false)
    private ShoppingCartService shoppingCartService;

    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 新增订单的业务方法
     * @param orders
     * @param userId
     */
    @Transactional
    public void addOrder(Orders orders,Long userId){
        //orders已经封装了：remark,addressBookId,payMethod
        //1.根据传递过来默认收获地址id获取地址对象AddressBook
        AddressBook addressBook = addressBookMapper.getById(orders.getAddressBookId());

        //2.根据传递过来的登录用户id获取购物车数据List<ShoppingCart>
        List<ShoppingCart> shoppingCartList = shoppingCartService.findAllByUserId(userId);

        //3.根据登录用户的id获取登录用户对象(得到用户别名写入到订单数据)
        User user = userMapper.findById(userId);

        //3.完善订单对象Orders数据
        Long oid = new Long(UUIDUtils.getUUIDInOrderId());
        orders.setId(oid);
        orders.setNumber(oid+"");
        orders.setStatus(1);  //1待付款
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());  //下单时间
        orders.setCheckoutTime(LocalDateTime.now()); //结账时间

        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(user.getName());  //登录用户的别名
        orders.setConsignee(addressBook.getConsignee());  //收件人

        //4.完善订单明细对象List<OrderDetail>数据
        ArrayList<OrderDetail> orderDetailList = new ArrayList<>();
        //定义总金额
        BigDecimal total = new BigDecimal("0");
        //遍历购物车集合数据，封装每个OrderDetail对象
        for (ShoppingCart shoppingCart : shoppingCartList){
            //将每个购物商品数据shoppingCart封装到OrderDetail对象中
            OrderDetail orderDetail = new OrderDetail();
            Long orderDetailId =new Long(UUIDUtils.getUUIDInOrderId());
            orderDetail.setId(orderDetailId);
            orderDetail.setName(shoppingCart.getName());  //菜品名字或套餐名字
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setOrderId(shoppingCart.getDishId());
            orderDetail.setDishId(orders.getId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setAmount(shoppingCart.getAmount());

            //封装好的orderDeail对象加入到orderDetailList
            orderDetailList.add(orderDetail);

            //累加每个商品的金额=数量*单价
            //multiply()这个是decimal类型的乘法方法
            //add()这个是decimal类型的加法方法
            total = total.add(orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber() + "")));
        }

        //注意：完善订单主表的总价
        orders.setAmount(total);

        //5.调用mapper新增订单主表数据
        orderMapper.save(orders);
        //6.调用mapper新增多条订单明细表数据
        orderDetailMapper.saveBatch(orderDetailList);

        shoppingCartMapper.deleteByUserId(userId);
    }
}
